package com.zx.tradeclient.ctrl.index1;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.zx.base.http.HttpUtils;
import com.zx.base.http.HttpUtils.CallBack;
import com.zx.base.lazylist.ImageLoader;
import com.zx.base.widget.LoadMoreListView;
import com.zx.base.widget.LoadMoreListViewWithProgress;
import com.zx.tradeclient.R;
import com.zx.tradeclient.app.TradeApp;
import com.zx.tradeclient.entity.AppCommentInfo;

public class NewsCommentActivity extends Activity {
	private List<AppCommentInfo> logs = new ArrayList<AppCommentInfo>();
	private int page = 1;
	private TradeApp app;
	private String newsid;
	private LoadMoreListViewWithProgress list_com_p;
	private LoadMoreListView list_com_progress;
	private ImageLoader imageLoader;
	private AppDetailCommentAdapter newscommentad;
	public Button header_left, header_right;
	public TextView header_title;

	private class ViewHolder {
		public ImageView user_icon;
		public TextView com_content;
		public TextView user_name;
		public TextView com_phone;
		public TextView com_num;
		public TextView com_date;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		app = (TradeApp) getApplicationContext();
		newsid = getIntent().getStringExtra("id");
		setContentView(R.layout.list_with_header);

		header_left = (Button) findViewById(R.id.header_left);
		header_right = (Button) findViewById(R.id.header_right);
		header_right.setVisibility(View.GONE);
		header_title = (TextView) findViewById(R.id.header_title);
		header_left.setText("返回");
		header_left.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				NewsCommentActivity.this.finish();
			}
		});
		DisplayMetrics metric = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(metric);

		list_com_p = (LoadMoreListViewWithProgress) findViewById(R.id.list);
		list_com_progress = list_com_p.getListView();
		// 生成动态数组，加入数据
		newscommentad = new AppDetailCommentAdapter(NewsCommentActivity.this, logs);

		list_com_progress.setAdapter(newscommentad);

		comTask();
	}

	private class AppDetailCommentAdapter extends BaseAdapter {

		private List<AppCommentInfo> app_com;
		private LayoutInflater mlinflater;
		private ViewHolder holder = null;

		public AppDetailCommentAdapter(Context context, List<AppCommentInfo> list) {
			this.mlinflater = LayoutInflater.from(context);
			this.app_com = list;
		}

		@Override
		public int getCount() {
			return app_com.size();
		}

		public Object getItem(int pos) {
			return pos;
		}

		public long getItemId(int pos) {

			return pos;
		}

		@Override
		public View getView(int position, View view, ViewGroup viewGroup) {

			AppCommentInfo com = app_com.get(position);

			if (view == null) {

				view = mlinflater.inflate(R.layout.app_detail_com_item, null);
				holder = new ViewHolder();
				holder.user_icon = (ImageView) view.findViewById(R.id.app_com_user_icon);
				holder.com_content = (TextView) view.findViewById(R.id.app_com_content);

				holder.user_name = (TextView) view.findViewById(R.id.app_com_user_name);

				holder.com_phone = (TextView) view.findViewById(R.id.app_com_phone);

				holder.com_num = (TextView) view.findViewById(R.id.app_com_num);

				holder.com_date = (TextView) view.findViewById(R.id.app_com_date);

				view.setTag(holder);
			} else {
				holder = (ViewHolder) view.getTag();
			}
			holder.com_content.setText(com.getCom_content());
			holder.user_name.setText(com.getUser_name());
			holder.com_phone.setText(com.getUser_phone());
			holder.com_num.setText(com.getCom_num() + "楼");
			holder.com_date.setText(com.getCom_date());

			Float r = Float.parseFloat(com.getCom_rat());

			return view;

		}

	}

	private void comTask() {

		Map<String, String> map1 = new HashMap<String, String>();
		map1.put("act", "commentsList");
		map1.put("id", newsid); // app_id
		map1.put("type", "1");

		HttpUtils.post(map1, new CallBack() {

			@Override
			public void onSuccess(JSONObject response) {
				parseComJson(response);
			}

			@Override
			public void onFailure(Throwable e) {
				// TODO Auto-generated method stub

			}
		});

	}

	private void parseComJson(JSONObject response) {

		try {
			String succeed = response.getString("succeed");
			if (succeed.equals("000")) {
				String data = response.getString("dataList");
				JSONArray list = new JSONArray(data);
				for (int i = 0; i < list.length(); i++) {
					JSONObject log = list.getJSONObject(i);
					AppCommentInfo com_info = new AppCommentInfo();

					if (log.getString("userId") == null) {
						com_info.setCom_id("");
					} else {
						com_info.setCom_id(log.getString("userId"));
					}

					if (null == log.getString("userImg")) {
						com_info.setUser_icon("");
					} else {
						com_info.setUser_icon(log.getString("userImg"));
					}

					if (null == log.getString("userName")) {
						com_info.setUser_name("");
					} else {
						com_info.setUser_name(log.getString("userName"));
					}

					if (null == log.getString("source")) {
						com_info.setUser_phone("");
					} else {
						com_info.setUser_phone("来自：" + log.getString("source"));
					}

					com_info.setCom_num(Integer.toString(i + 1));

					if (null == log.getString("addtime")) {
						com_info.setCom_date("");
					} else {
						com_info.setCom_date(log.getString("addtime"));
					}

					if (null == log.getString("score")) {
						com_info.setCom_rat("");
					} else {
						com_info.setCom_rat(log.getString("score"));
					}

					if (null == log.getString("content")) {
						com_info.setCom_content("");
					} else {
						com_info.setCom_content(log.getString("content"));
					}

					logs.add(com_info);
				}

			} else if (succeed.equals("002")) {

			} else {

			}
			newscommentad.notifyDataSetChanged();
			list_com_p.show();
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}

}
